package com.hrt.base;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.hrt.bean.UsersInfor;
import com.hrt.util.PageUtils;
import com.hrt.util.WebParams;

@Controller
public class BaseController<E> {

	private Long totalCount = new Long(0);

	private Integer pageCount;

	private int pageSize = 10;

	private int pageIndex;

	protected PageUtils<E> page;

	protected Class<E> clazz;

	protected List<E> list;

	public List<E> getList() {
		return list;
	}

	public void setList(List<E> list) {
		this.list = new ArrayList();
		this.list = list;
	}

	public BaseController() {

	}

	private void init() {

	}

	public PageUtils<E> getPage() {

		if (getPageIndex() == 0) {
			setPageIndex(1);
		}
		page = new PageUtils<E>();
		page.setPageCount(getPageCount());
		page.setPageIndex(getPageIndex());
		page.setPageSize(getPageSize());
		page.setTotalCount(getTotalCount());
		page.setResultList((List<E>) getList());

		return page;
	}

	public void setPage(PageUtils<E> page) {
		this.page = page;
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Class<E> getClazz() {
		return clazz;
	}

	public void setClazz(Class<E> clazz) {
		this.clazz = clazz;
	}

	/**
	 *
	 * @param totalCount
	 * @param pageSize
	 * @return
	 */
	public Integer computePageCount(Long totalCount, int pageSize) {
		if (totalCount==null ||totalCount == 0) {
			return 1;
		}
		if (totalCount % pageSize == 0) {
			return (int) (totalCount / pageSize);
		} else {
			return (int) (totalCount / pageSize) + 1;
		}

	}

	public Integer computePageCount() {
		if (totalCount == 0) {
			return 1;
		}
		if (getTotalCount() % pageSize == 0) {
			return (int) (getTotalCount() / pageSize);
		} else {
			return (int) (getTotalCount() / pageSize) + 1;
		}

	}

	public static Map<String, Object> transBean2Map(Object obj) {

		if (obj == null) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();

				// 过滤class属性
				if (!key.equals("class")) {
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					Object value = getter.invoke(obj);

					map.put(key, value);
				}

			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}

		return map;

	}

	protected UsersInfor getOnlineManager(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			return (UsersInfor) request.getSession().getAttribute(
					WebParams.ON_LINE_MANAGER);
		}

		return null;
	}

	protected void setOnlineManager(UsersInfor user,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			request.getSession().removeAttribute(WebParams.ON_LINE_MANAGER);
		}
		request.getSession().setAttribute(WebParams.ON_LINE_MANAGER, user);
	}

}
